package com.guodu.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.ViewGroupContractDetailDao;
import com.guodu.entity.ViewFunitsContractDetail;
import com.guodu.entity.ViewGroupContractDetail;
import com.guodu.entity.ViewUnitsContractDetail;
import com.guodu.entity.ViewZbContractDetail;
import com.guodu.vo.ViewVo;

@Repository("viewGroupContractDetailDao")
public class ViewGroupContractDetailDaoImpl extends BaseDao implements ViewGroupContractDetailDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<ViewGroupContractDetail> findByVo(ViewVo viewVo,Long contractType) {
		// TODO Auto-generated method stub
		
		StringBuffer hql = new StringBuffer();
		if(contractType==1L){
		hql.append("from ViewGroupContractDetail where 1=1");
		}else if(contractType==2L){
			hql.append("from ViewFunitsContractDetail where 1=1");
		}else if(contractType==3L){
			hql.append("from ViewUnitsContractDetail where 1=1");
		}else if(contractType==4L){
			hql.append("from ViewZbContractDetail where 1=1");
		}
		if(viewVo!=null){
			if(viewVo.getFunitsId()!=null){
				hql.append(" and funitsId = "+viewVo.getFunitsId());
			}
			if(viewVo.getUnitsId()!=null){
				hql.append(" and unitsId = "+viewVo.getUnitsId());
			}
			if(viewVo.getTime()!=null&&viewVo.getTime().trim().length()>0){
				hql.append(" and year = '"+viewVo.getTime().split("-")[0]+"' and month = '"+viewVo.getTime().split("-")[1]+"'");
			}
		}
		if(contractType==1L){
			return super.find(hql.toString());
		}else if(contractType==2L){
			List<ViewFunitsContractDetail> fviews = super.find(hql.toString());
			List<ViewGroupContractDetail> views = new ArrayList<ViewGroupContractDetail>();
			for(ViewFunitsContractDetail view : fviews){
				ViewGroupContractDetail groupView = new ViewGroupContractDetail();
				groupView.setAllContractCount(view.getAllContractCount());
				groupView.setAllContractMoney(view.getAllContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setContractMoney(view.getContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setFunitsId(view.getFunitsId());
				groupView.setId(view.getId());
				groupView.setMonth(view.getMonth());
				groupView.setPurchaseType(view.getPurchaseType());
				groupView.setUnitsId(view.getUnitsId());
				groupView.setYear(view.getYear());
				views.add(groupView);
			}
			return views;
		}else if(contractType==3L){
			List<ViewUnitsContractDetail> fviews = super.find(hql.toString());
			List<ViewGroupContractDetail> views = new ArrayList<ViewGroupContractDetail>();
			for(ViewUnitsContractDetail view : fviews){
				ViewGroupContractDetail groupView = new ViewGroupContractDetail();
				groupView.setAllContractCount(view.getAllContractCount());
				groupView.setAllContractMoney(view.getAllContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setContractMoney(view.getContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setFunitsId(view.getFunitsId());
				groupView.setId(view.getId());
				groupView.setMonth(view.getMonth());
				groupView.setPurchaseType(view.getPurchaseType());
				groupView.setUnitsId(view.getUnitsId());
				groupView.setYear(view.getYear());
				views.add(groupView);
			}
			return views;
		}else if(contractType==4L){
			List<ViewZbContractDetail> fviews = super.find(hql.toString());
			List<ViewGroupContractDetail> views = new ArrayList<ViewGroupContractDetail>();
			for(ViewZbContractDetail view : fviews){
				ViewGroupContractDetail groupView = new ViewGroupContractDetail();
				groupView.setAllContractCount(view.getAllContractCount());
				groupView.setAllContractMoney(view.getAllContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setContractMoney(view.getContractMoney());
				groupView.setContractCount(view.getContractCount());
				groupView.setFunitsId(view.getFunitsId());
				groupView.setId(view.getId());
				groupView.setMonth(view.getMonth());
				groupView.setPurchaseType(view.getPurchaseType());
				groupView.setUnitsId(view.getUnitsId());
				groupView.setYear(view.getYear());
				views.add(groupView);
			}
			return views;
		}
		return super.find(hql.toString());
	}

}
